package com.yhsimon.controller;

import com.yhsimon.entity.Order;
import com.yhsimon.entity.OrderVO;
import com.yhsimon.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.uid;


@RestController
@RequestMapping("/order")
public class OrderHandler {

    @Autowired
    private OrderRepository orderRepository;

    @Value(value = "${server.port}")
    private String port;

    @GetMapping("/index")
    public String index() {
        return "order端口号：" + this.port;
    }

    @GetMapping("/findAllByUId/{uId}/{index}/{limit}")
    public OrderVO findAllByUId(@PathVariable("uId") int uId, @PathVariable("index") int index, @PathVariable("limit") int limit) {
        OrderVO orderVO = new OrderVO(0, "", orderRepository.countById(uId), orderRepository.findAllByUId(uId, index, limit));
        return orderVO;
    }

    @GetMapping("/findAllByState/{index}/{limit}")
    public OrderVO findAllByState(@PathVariable("index") int index, @PathVariable("limit") int limit) {
        List<Order> orders = orderRepository.findAllByState(index, limit);
        System.out.println(orders.toString());
        OrderVO orderVO = new OrderVO(0, "", orderRepository.countByState(0), orders);
        return orderVO;
    }

    @GetMapping("/findAll/{index}/{limit}")
    public OrderVO findAll(@PathVariable("index") int index, @PathVariable("limit") int limit) {
        List<Order> orders = orderRepository.findAll(index, limit);
        System.out.println("所有orders:"+orders.toString());
        OrderVO orderVO = new OrderVO(0, "", orderRepository.count(), orders);
        return orderVO;
    }

    @PutMapping("/updateState/{id}/{state}/{aid}")
    public void updateState(@PathVariable("id") int id, @PathVariable("state") int state, @PathVariable("aid") int aid) {
        System.out.println(id+"----"+"aid:"+aid+"state="+state);
        orderRepository.updateState(id, state, aid);
    }

    @PostMapping("/save")
    public void save(@RequestBody Order order) {
        orderRepository.save(order);
    }

    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") int id) {
        orderRepository.deleteById(id);
    }


}
